<template>
    <div>
        <el-form :model="User" status-icon :rules="rules" ref="User" label-width="140px" class="demo-User">
            <el-form-item label="userName" prop="userName">
                <el-input v-model="User.userName"></el-input>
            </el-form-item>
            <el-form-item label="password" prop="password">
                <el-input type="password" v-model="User.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="repeat password" prop="repeat_password">
                <el-input type="password" v-model="User.repeat_password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="name" prop="name">
                <el-input v-model="User.name"></el-input>
            </el-form-item>
            <el-form-item label="age" prop="age">
                <el-input v-model="User.age"></el-input>
            </el-form-item>
            <el-form-item label="email" prop="email">
                <el-input v-model="User.email"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('User')">确认修改</el-button>
                <el-button @click="resetForm('User')">清空</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Update",
        data() {
            let checkAge = (rule, value, callback) => {

                if (!(/^[+]?(\d+)$|^[+]?(\d+\.\d+)$/).test(value)) {
                    callback(new Error('请输入数字'));
                } else {
                    callback()
                }
            };
            let checkPassword = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.User.password) {
                    callback(new Error('两次输入密码不一致'));
                } else {
                    callback();
                }
            };
            return {
                User: {
                    userName: '',
                    password: '',
                    repeat_password: '',
                    name: '',
                    age: '',
                    email: '',
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 32, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 16, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                    ],
                    repeat_password: [
                        { required: true, validator:checkPassword, trigger: 'blur' }
                    ],
                    name: [],
                    age: [],
                    email: [],
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.put('http://localhost:8081/user/update',_this.User).then(function (response) {
                            if(response.data>0){
                                alert('成功修改'+response.data+'条记录');
                            }
                            else{
                                alert('修改失败');
                            }
                        });
                    } else {
                        console.log('不可修改');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>